﻿
function addMsg(obj, msg, css) {
    var thisObj = $(obj);
    thisObj.text(msg).removeClass().addClass(css);
}

//提示插件
jQuery.fn.extend({
    //出错信息
    addShowMsg: function (msg) {
        addMsg(this, msg, "sign_info");
    },
    //提示信息
    addInfoMsg: function (msg) {
        addMsg(this, msg, "tip_info");
    }
});

function loginsite() {
    if ($.trim(username.val()).length == 0 || $.trim(username.val()) == "用户名或手机号") {
        showInfo.addShowMsg("请输入用户名！");
        return false;
    }
    else if (password.val().length == 0) {
        showInfo.addShowMsg("请输入密码！");
        return false;
    }
    var autoLogin = chkAuto.attr("checked") == "checked" ? "1" : "0";
    $.ajax({
        url: "/Ashx/Login/LoginHandler.ashx",
        data: { userName: encodeURIComponent(username.val()), password: encodeURIComponent(password.val()), safeCode: safeCode.val(), autoLogin: autoLogin },
        beforeSend: function () {
            showInfo.addInfoMsg("正在登录...");
        },
        type: "POST",
        dataType: "json",
        success: function (result) {
            if (result.ReturnInfo.toString() == "success" && result.IsLogin.toString() == "true") {
                showInfo.addInfoMsg("登录成功！正在跳转...");
                var returnUrl = Request("returnUrl");
                if (returnUrl.length <= 0 || returnUrl == null || returnUrl == undefined) {
                    returnUrl = "/User/Default.aspx";
                }
                window.location.href = decodeURIComponent(returnUrl);
            }
            else {
                login.attr("disabled", false);
                ChangeSafeCode();
                showInfo.addShowMsg(result.ReturnInfo.toString());
            }
        },
        error: function (httpRequest, textStatus, errorThrown) {
            login.attr("disabled", false);
            showInfo.addShowMsg("系统出错，请重新登录！");
            login.show();
        }
    });
}

//定义
var showInfo;
var login;
var username;
var password;
var safeCode;
$(function () {

    //初始化
    showInfo = $("#showInfo");
    chkAuto = $("#chkAuto");
    login = $("#login");
    username = $("#username");
    safeCode = $("#safeCode");
    password = $("#password");

    login.click(function () {
        loginsite();
    });

    username.keypress(function (event) {
        if (event.keyCode == "13") {
            loginsite();
        }
    }).focus();

    password.keypress(function (event) {
        if (event.keyCode == "13") {
            loginsite();
        }
    });
});